import { Component, OnInit } from '@angular/core';
import { BladeContainerInterface } from "../../../../shared/blade/blade-container.interface";
import { Blade } from "../../../../shared/blade/blade.model";
import { Observable } from "rxjs/Observable";
import { FormBuilder, FormGroup, Validators, FormControl } from "@angular/forms";
import { SimpleChanges } from "@angular/core/core";
import { Command } from "../../../../shared/toolbar.service";
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { MyValidators } from "../../../../utils/validator/rang";

import { UserAlarmconfig, ForUserAlarmconfig } from "../../../../entity/user_alarmconfig.model";
import { UserAlarmconfigService } from "../../../../service/user_alarmconfig.service";

import { Setofvalue } from "../../../../entity/setofvalue.model";
import { SetofkeyDict } from "../../../../entity/setof-dict.model";
import { ApplicationService } from "../../../../service/application.service";

@Component({
	selector: 'app-alarm-basic-config-edit',
	templateUrl: './alarm-basic-config-edit.component.html',
	styleUrls: ['./alarm-basic-config-edit.component.css']
})
export class AlarmBasicConfigEditComponent extends BladeContainerBase<UserAlarmconfig> {

	public audioParameters: Setofvalue[] = [];
	public alarmconfigData: UserAlarmconfig[] = [];
	public original: UserAlarmconfig[] = [];
	private configs: UserAlarmconfig[] = [];

	public alarmTimeConfig: UserAlarmconfig[] = [];
	public currentAlarm: UserAlarmconfig;

	public len: number;
	public isNum: boolean;

	constructor(
		private fb: FormBuilder,
		private userAlarmconfigService: UserAlarmconfigService,
		public application: ApplicationService,
	) {
		super(fb);
		this.audioParameters = application.setKeyDict["AudioParameter"];
	}

	ngOnInit() {
		this.isNum = typeof (this.currentEntity.alarmConfigValue) === 'number' ? true : false;
		if (!this.isNew) {
			this.commands = [
				new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
				new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
			]
		}
		super.ngOnInit();
	}

	/**
	 * 初始化数据
	 * 
	 * 
	 * @memberof TurbstatusValueListComponent
	 */
	initData(): any | Observable<any> | Promise<any> {
		this.formPatchValue(this.currentEntity);
	}

	exeSaveChange(blade: any): void {
		this.saveChange();
	}
	canSaveChange(blade: any): boolean {
		return this.hasChange() && !this.hasError();
	}
	exeReset(blade: any): void {
		return this.formGroup.reset(this.currentEntity);
	}
	canReset(blade: any): boolean {
		return this.hasChange();
	}

	/**
	  * 创建表单
	  * 
	  * 
	  * @memberof DicKeyEditComponent
	  */
	createForm() {
		this.formGroup = this.fromBuider.group({
			"alarmConfigDesc": this.currentEntity.alarmConfigDesc,
			"alarmConfigValue": this.currentEntity.alarmConfigValue
		});
	}
	/**
	* 设置表单错误对象
	* 
	* 
	* @memberof BladeContainerBase
	*/
	setFormError(): void {
		this.formErrors = {
			'alarmConfigDesc': '',
		};
	}

	/**
	* 设置表单验证信息
	* 
	* 
	* @memberof BladeContainerBase
	*/
	setValidationMessages(): void {
		this.validationMessages = {
			'alarmConfigDesc': {
				'required': '名称。',
			},
		};
	}

	/**
	 * 
	 * 
	 * 
	 * @memberof TurbstatusEditComponent
	 */
	saveChange(): any | Promise<any> | Observable<any> {
		if (this.canSaveChange(this.currentBlade)) {
			this.changeCurrent();
			let userConfig = {
				userAlarmConfigId: this.currentEntity.userAlarmConfigId,
				alarmConfigId: this.currentEntity.alarmConfigId,
				userId: this.currentEntity.userId,
				alarmConfigValue: this.currentEntity.alarmConfigValue
			}
			if (userConfig.userAlarmConfigId) {
				return this.userAlarmconfigService.update(userConfig)
					.then(res => {
						if (res) {
							(this.parentBlade.bladeContainerInstance).refresh(res);
							this.bladeService.closeBlade(this.currentBlade);
							return true;
						}
					})
					.catch(error => this.bladeData.error = error)
					.then(res => this.bladeData.isLoading = false);
			} else {
				return this.userAlarmconfigService.create(userConfig)
					.then(res => {
						if (res) {
							(this.parentBlade.bladeContainerInstance).refresh(res);
							this.bladeService.closeBlade(this.currentBlade);
							return true;
						}
					})
					.catch(error => this.bladeData.error = error)
					.then(res => this.bladeData.isLoading = false);
			}

		}
	}

	changeCurrent() {
		this.bladeData.isLoading = true;
		const formValue = this.formGroup.value as UserAlarmconfig;
		this.currentEntity.alarmConfigValue = formValue.alarmConfigValue;
	}

}




// WEBPACK FOOTER //
// ./src/app/module/basic/alarm-basic-config/alarm-basic-config-edit/alarm-basic-config-edit.component.ts